/**
 * jQuery Pager Plugin
 * http://nunax.com/jquery/pager/
 *
 * Copyright 2011, Alfred Yang, era.yeung@gmail.com
 * Released under the MIT License.
 *
 */
(function($){
	
	$.pageBase = {};
	$.pageBase.opts = {
		size : 5,
		page : 1,
		start : 1,
		max : 5,
		first : '&laquo;',
		prev : '&lsaquo;',
		next : '&rsaquo;',
		last : '&raquo;',
		style : 'pager_style'
	}
	$.pageBase.span = function(pageText, clasName){
		return $('<span>' + pageText + '</span>').addClass(clasName);
	}
	$.pageBase.getPager = function(currentPage, lastPage, maxShow){
		var halfShow = Math.floor(maxShow / 2);
		var limit = lastPage - maxShow + 1;
		var start = currentPage > halfShow ? Math.max(Math.min(currentPage - halfShow, limit), 0 ) : 0;
		var end = currentPage > halfShow ? Math.min(currentPage + halfShow - 1 + maxShow % 2, lastPage) : Math.min(maxShow, lastPage);
		return {start : start, end : end};
	}
	
	$.fn.pager = function(settings){
		var opts = {
			url : '#',
			replace : /__page__/
		}
		$.extend(opts, $.pageBase.opts, settings);
		if(opts.size < 1) opts.size = 1;
		if(opts.page < opts.start || opts.page > (opts.start + opts.size - 1)) opts.page = opts.start;
		var lk = function(pageText, page){
			return $('<a>' + pageText + '</a>').attr('href', opts.url.replace(opts.replace, page));
		}
		var initPager = function(page, container){
			var cont = $('<div class="' + opts.style + '" style="display:inline-block;"></div>');
			var idxFirst = opts.start;
			var idxLast = opts.start + opts.size - 1;
			var pos = $.pageBase.getPager(page, idxLast, opts.max);
			if(page == idxFirst){
				cont.append($.pageBase.span(opts.first, 'disabled'));
				cont.append($.pageBase.span(opts.prev, 'disabled'));
			}else{
				cont.append(lk(opts.first, idxFirst));
				cont.append(lk(opts.prev, page > idxFirst ? page - 1 : idxFirst));
			}
			for(var i = idxFirst; i <= idxLast; i = i + 1){
				if(i < pos.start || i > pos.end) continue;
				if(page == i) cont.append($.pageBase.span(i, 'current'));
				else cont.append(lk(i, i));
			}
			if(page == idxLast){
				cont.append($.pageBase.span(opts.next, 'disabled'));
				cont.append($.pageBase.span(opts.last, 'disabled'));
			}else{
				cont.append(lk(opts.next, page < idxLast ? page + 1 : idxLast));
				cont.append(lk(opts.last, idxLast));
			}
			container.empty();
			container.append(cont);
		}
		return this.each(function(){initPager(opts.page, $(this));});
	}
	
	$.fn.ajaxPager = function(settings){
		var opts = {
			init : true,
			url : '',
			type : 'post',
			dataType : 'json',
			param : 'page',
			params : {},
			ajaxStart : function(){return false;},
			callback : function(data){return false;},
			errorCallback : function(){return false;},
			ajaxFunction : function(page){ajaxCall(page);}
		}
		$.extend(opts, $.pageBase.opts, settings);
		if(opts.size < 1) opts.size = 1;
		if(opts.page < opts.start || opts.page > (opts.start + opts.size - 1)) opts.page = opts.start;
		var ajaxCall = function(page){
			opts.ajaxStart();
			var pageDictVar = {};
			eval('pageDictVar.' + opts.param + ' = ' + page);
			$.ajax({
				type : opts.type,
				url : opts.url, 
				dataType : opts.dataType,
				data : $.extend(pageDictVar, opts.params), 
				success : function(data, textStatus, jqXHR){
					opts.callback(data, textStatus, jqXHR);	
				},
				error : function(jqXHR, textStatus, errorThrown){
					opts.errorCallback(jqXHR, textStatus, errorThrown);
				}
			});
		}
		var gotoPage = function(event){
			var page = event.data.page;
			initPager(page, event.data.container);
			opts.ajaxFunction(page);
			return false;
		}
		var lk = function(pageText, page, container){
			return $('<a>' + pageText + '</a>').attr('href', '').bind('click', {page : page, container : container}, gotoPage);
		}
		var initPager = function(page, container){
			var cont = $('<div class="' + opts.style + '" style="display:inline-block;"></div>');
			var idxFirst = opts.start;
			var idxLast = opts.start + opts.size - 1;
			var pos = $.pageBase.getPager(page, idxLast, opts.max);
			if(page == idxFirst){
				cont.append($.pageBase.span(opts.first, 'disabled'));
				cont.append($.pageBase.span(opts.prev, 'disabled'));
			}else{
				cont.append(lk(opts.first, idxFirst, container));
				cont.append(lk(opts.prev, page > idxFirst ? page - 1 : idxFirst, container));
			}
			for(var i = idxFirst; i <= idxLast; i = i + 1){
			if(i < pos.start || i > pos.end) continue;
				if(page == i) cont.append($.pageBase.span(i, 'current'));
				else cont.append(lk(i, i, container));
			}
			if(page == idxLast){
				cont.append($.pageBase.span(opts.next, 'disabled'));
				cont.append($.pageBase.span(opts.last, 'disabled'));
			}else{
				cont.append(lk(opts.next, page < idxLast ? page + 1 : idxLast, container));
				cont.append(lk(opts.last, idxLast, container));
			}
			container.empty();
			container.append(cont);
		}
		return this.each(function(){initPager(opts.page, $(this));if(opts.init){opts.ajaxFunction(1);}});
	}
})(jQuery);